<?php
/*
*
* @package phpBB3 Portal a.k.a canverPortal  ( www.phpbb3portal.com )
* @version $Id: forum_tree.php,v 1.0 2009/09/07 09:52:00 Gorlum Exp $
* @copyright (c) Gorlum
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

if (!defined('IN_PHPBB') or !defined('IN_PORTAL'))
{
	die('Hacking attempt');
	exit;
}

$default_list_root = $config['portal_forum_tree_forums'] ? $config['portal_forum_tree_forums'] : 0; // Tracker Root - only one for now. Use SQL "IN" construction to add more

$list_root = request_var('lr', 0);
$list_root ? '' : $list_root=$default_list_root;

make_forumtree($list_root);

$template->assign_vars(array(
	'S_DISPLAY_FORUMTREE' => true,
	'S_PORTAL_FTREE_HEADER' => $user->lang['PORTAL_FTREE_HEADER'],
));


/**
* Generate ForumTree
* Based on make_jumpbox function from phpBB3 copyright (c) 2005 phpBB Group
* input: ID of root forum, selected forum, use acl_list
*/
function make_forumtree($list_root = '', $acl_list = false)
{
	global $config, $auth, $template, $user, $db, $phpEx, $phpbb_root_path;

	$text_length = $config['portal_forum_tree_forum_name_length']; // Max len of forum name
	$padding_store = array();
	$sql_where='';

	if ($list_root)
	{
		$sql = 'SELECT left_id, right_id, parent_id
			FROM ' . FORUMS_TABLE . ' WHERE forum_id in (' . $db->sql_escape($list_root) . ')';
		$result = $db->sql_query($sql, 600);

		while ($row1 = $db->sql_fetchrow($result))
		{
			if ($row1)
			{
				if ($sql_where)
				{
					$sql_where .= ' OR ';
				};
				// This will show only selected branch and all sub-branches
				//$sql_where .= ' ((left_id >= ' . $row1["left_id"] . ') AND (right_id <= ' . $row1["right_id"] . ')) ';

				// This will show all sub-branches of selected branch and path to root
				$sql_where .= ' ((left_id < ' . $row1["right_id"] . ') AND (right_id > ' . $row1["left_id"] . ')) ';
				$padding_store[$row1["parent_id"]] = 0;
			};
		}
		if ($sql_where)
		{
			$sql_where = ' WHERE ' . $sql_where;
		};
	}
	else
	{
		$padding_store[0] = 0;
	};

	$sql = 'SELECT forum_id, forum_name, parent_id, forum_type, left_id, right_id
		FROM ' . FORUMS_TABLE . $sql_where . '
			ORDER BY left_id ASC';
	$result = $db->sql_query($sql, 600);

	$right = $padding = 0;
	$display_forumtree = false;
	$iteration = 0;

	// Sometimes it could happen that forums will be displayed here not be displayed within the index page
	// This is the result of forums not displayed at index, having list permissions and a parent of a forum with no permissions.
	// If this happens, the padding could be "broken"

	while ($row = $db->sql_fetchrow($result))
	{
		if ($row['left_id'] < $right)
		{
			$padding++;
			$padding_store[$row['parent_id']] = $padding;
		}
		else if ($row['left_id'] > $right + 1)
		{
			// Ok, if the $padding_store for this parent is empty there is something wrong. For now we will skip over it.
			// @todo digging deep to find out "how" this can happen.
			$padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : $padding;
		}

		$right = $row['right_id'];

		if ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']))
		{
			// Non-postable forum with no subforums, don't display
			continue;
		}

		if (!$auth->acl_get('f_list', $row['forum_id']))
		{
			// if the user does not have permissions to list this forum skip
			continue;
		}

		if ($acl_list && !$auth->acl_gets($acl_list, $row['forum_id']))
		{
			continue;
		}

		$len_check = $row['forum_name'];
		if (($text_length != 0) && (mb_strlen($len_check) > $text_length))
		{
			$len_check = mb_substr($len_check, 0, $text_length);
			$len_check .= '...';
		};

		$template->assign_block_vars('forumtree_forums', array(
			'FORUM_ID'    => append_sid($phpbb_root_path."portal.{$phpEx}?lr={$row['forum_id']}"),
			'FORUM_NAME'  => $len_check,
			//      'FORUM_NAME'  => $len_check . ' ID: ' . $row['forum_id'] . ' left=' . $row['left_id'] . ' right=' . $row['right_id'] . ' ' . $row1['left_id'] . '+' . $row1['right_id'],
			//      'FORUM_NAME'    => 'ID: ' . $row['forum_id'] . ' left=' . $row['left_id'] . ' right=' . $row['right_id'] . ' ' . $row1['left_id'] . '+' . $row1['right_id'],
			//'SELECTED'    => ($row['forum_id'] == $forum_id) ? ' selected="selected"' : '',
			'S_FORUM_COUNT' => $iteration,
			'S_IS_CAT'    => ($row['forum_type'] == FORUM_CAT) ? true : false,
			'S_IS_LINK'   => ($row['forum_type'] == FORUM_LINK) ? true : false,
			'S_IS_POST'   => ($row['forum_type'] == FORUM_POST) ? true : false)
		);

		for ($i = 0; $i < $padding; $i++)
		{
			$template->assign_block_vars('forumtree_forums.level', array());
		}
		$iteration++;
	}
	$db->sql_freeresult($result);
	unset($padding_store);

	$template->assign_vars(array(
		'S_PORTAL_FTREE_HEADER' => $user->lang['PORTAL_FTREE_HEADER'],
		'S_DISPLAY_FORUMTREE' => true,
		//'S_FORUMTREE_ACTION'  => $action
		)
	);

	return;
}

?>
